package com.we.wecc.aw.mid.impl;

import com.we.wecc.aw.db.mapper.AwardMapper;
import com.we.wecc.aw.db.mapper.AwardSpeechMapper;
import com.we.wecc.aw.db.model.Award;
import com.we.wecc.aw.db.model.AwardSpeech;
import com.we.wecc.aw.db.model.AwardSpeechExample;
import com.we.wecc.aw.mid.model.in.AwardSpeechIn;
import com.we.wecc.aw.mid.model.out.AwardSpeechOut;
import com.we.wecc.aw.mid.service.AwardSpeechService;
import com.we.wecc.us.db.mapper.UserMapper;
import com.x.share.db.model.Pagination;
import com.x.share.mid.exception.HelperException;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AwardSpeechServiceImpl implements AwardSpeechService {

    @Resource
    AwardSpeechMapper awardSpeechMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    AwardMapper awardMapper;

    @Override
    public AwardSpeech saveAwardSpeech(AwardSpeechIn in) {
        AwardSpeech as = in.toAwardSpeech();
        Award award = awardMapper.selectByPrimaryKey(as.getAwardId());
        if(award==null){
            throw new HelperException("该活动不存在！");
        }
        as.setCreateTime(new Date());
        if (as.getId() != null && as.getId() > 0) {
            AwardSpeech awardSpeech = awardSpeechMapper.selectByPrimaryKey(as.getId());
            if (awardSpeech == null) {
                throw new HelperException("该获奖感言不存在！");
            }
            awardSpeechMapper.updateByPrimaryKeySelective(as);
        } else {
            awardSpeechMapper.insertSelective(as);
        }
        return as;
    }

    @Override
    public Boolean deleteAwardSpeech(int id) {
        Boolean flag = false;
        AwardSpeech ua = awardSpeechMapper.selectByPrimaryKey(id);
        if(ua==null){
            throw new HelperException("该获奖感言不存在！");
        }
        ua.setDelFlag(-1);
        int i = awardSpeechMapper.updateByPrimaryKeySelective(ua);
        if(i == 1){
            flag = true;
        }
        return flag;
    }

    @Override
    public Pagination<AwardSpeechOut> getAwardSpeechListByAward(int awardId, Integer pageNo, Integer pageSize) {
        if(awardId <= 0){
            throw new HelperException("该活动不存在！");
        }
        AwardSpeechExample ex = new AwardSpeechExample();
        ex.or().andAwardIdEqualTo(awardId).andDelFlagEqualTo(0);
        ex.setOrderByClause("createTime desc");
        RowBounds rb = new RowBounds(Pagination.offset(pageNo, pageSize), Pagination.limit(pageSize));
        List<AwardSpeechOut> listAwardSpeech = new ArrayList<>();
        List<AwardSpeech> list = awardSpeechMapper.selectByExampleWithRowbounds(ex,rb);
        list.forEach(s -> {
            AwardSpeechOut awardSpeechOut = new AwardSpeechOut();
            BeanUtils.copyProperties(s, awardSpeechOut);
            listAwardSpeech.add(awardSpeechOut);
        });
        int count = (int)awardSpeechMapper.countByExample(ex);
        Pagination<AwardSpeechOut> page = new Pagination<>(pageNo, pageSize);
        page.setItems(listAwardSpeech);
        page.setRecords(count);
        return page;
    }

    @Override
    public Pagination<AwardSpeechOut> getAwardSpeechList(Integer pageNo, Integer pageSize) {
        AwardSpeechExample ex = new AwardSpeechExample();
        ex.or().andDelFlagEqualTo(0);
        ex.setOrderByClause("createTime desc");
        RowBounds rb = new RowBounds(Pagination.offset(pageNo, pageSize), Pagination.limit(pageSize));
        List<AwardSpeechOut> listAwardSpeech = new ArrayList<>();
        List<AwardSpeech> list = awardSpeechMapper.selectByExampleWithRowbounds(ex,rb);
        list.forEach(s -> {
            AwardSpeechOut awardSpeechOut = new AwardSpeechOut();
            BeanUtils.copyProperties(s, awardSpeechOut);
            listAwardSpeech.add(awardSpeechOut);
        });
        int count = (int)awardSpeechMapper.countByExample(ex);
        Pagination<AwardSpeechOut> page = new Pagination<>(pageNo, pageSize);
        page.setItems(listAwardSpeech);
        page.setRecords(count);
        return page;
    }
}
